<template>
  <div class="reg">
    <el-row :gutter="20" type="flex" justify="center">
      <el-col :span="6">
        <el-form label-width="80px" :model="user" :rules="rules">
          <el-form-item label="用户名">
            <el-input type="text" v-model="user.username" prop="username"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input type="password" v-model="user.pwMd5" prop="password"></el-input>
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="user.name"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="register">注 册</el-button>
            <el-button type="info" style="margin-right: 10px" @click="resetForm">清 空</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import router from "../../router";

export default {
  name: "Register",
  data() {
    return {
      user:{
        username:"",
        pwMd5:'',
        name:''
      },
      rules: {
        username: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    register() {
      this.$axios.post("/user/register",this.user).then(res => {
        if(res.data.code == 0) {
          this.$message.success(res.data.message)
          router.push('/main')
        } else if (res.data.code ==701) {
          this.$message.error(res.data.message)
        }
      })
    },
    resetForm() {
      this.user =  {
              username:"",
            pwMd5:'',
            name:''
      }
    }
  }
}
</script>

<style scoped>

.reg {
  width: 100%;
  height: 100%;
  background-color: white;
}
.el-form {
  margin-top: 280px;
}

</style>